<?php

namespace app\store\controller\market;

use app\store\controller\BaseController;
use app\store\service\ComboService;
use app\store\service\StoreUserGradeService;
use think\exception\ValidateException;
use think\facade\View;

class ComboController extends BaseController
{
    public function index()
    {
        $comboService = new ComboService();
        $list = $comboService->getComboList();

        return View::fetch('',['list' => $list]);
    }



    public function create()
    {
        $gradeOptions = StoreUserGradeService::getStoreUserGradeOptions();


        return View::fetch('',['gradeOptions' => $gradeOptions,]);
    }

    public function save()
    {
        $param = $this->request->param();

        try {
            validate('ComboValidate')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }


        $comboService = new ComboService();
        $result = $comboService->saveCombo($param);

        return  $result ? json(['code' => 1,'msg' => '保存成功!']) : json(['code' => 0, 'msg' => '保存失败!']);

    }


    public function edit()
    {
        $id = $this->request->param('id/d');

        $gradeOptions = StoreUserGradeService::getStoreUserGradeOptions();

        $comboService = new ComboService();
        $row = $comboService->findCombo($id);


        return View::fetch('',['gradeOptions' => $gradeOptions, 'row' => $row]);
    }


    public function update()
    {
        $param = $this->request->param();

        try {
            validate('ComboValidate.edit')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        $comboService = new ComboService();
        $comboService->updateCombo($param);

        return  json(['code' => 1,'msg' => '更新成功!']);
    }

    public function delete()
    {
        $id = $this->request->param('id/d');

        $comboService = new ComboService();
        $result = $comboService->deleteCombo($id);

        return  $result ? json(['code' => 1,'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }


}